package com.ukao.cashregister.ui.pickClothing;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.bigkoo.pickerview.TimePickerView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.mcxtzhang.commonadapter.rv.OnItemClickListener;
import com.ukao.cashregister.R;
import com.ukao.cashregister.adapter.OrderListAdapter;
import com.ukao.cashregister.base.IAdapter;
import com.ukao.cashregister.base.IPage;
import com.ukao.cashregister.base.MvpFragment;
import com.ukao.cashregister.base.PageWrapper;
import com.ukao.cashregister.bean.OrdersList;
import com.ukao.cashregister.listener.MyOnItemClickListener;
import com.ukao.cashregister.pesenter.OrderListPesenter;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.StackManager;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.view.OrderlistView;
import com.ukao.cashregister.widget.TwinklingLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 客户信息右边取衣
 */
public class PickClothingUserInfoRightFragment extends MvpFragment<OrderListPesenter> implements OrderlistView {


    @BindView(R.id.orders_list_recycler)
    RecyclerView mRecycler;
    Unbinder unbinder;
    @BindView(R.id.refresh_layout)
    TwinklingLayout refreshLayout;
    @BindView(R.id.fragment_create_orders_user_info_right_toolbar)
    Toolbar mtoolbar;
    @BindView(R.id.title_layout)
    LinearLayout titleLayout;
    @BindView(R.id.showAllbtn)
    Button showAllbtn;
    private TimePickerView pvCustomTime;
    private List<OrdersList.PageInfoBean.ListBean> items = new ArrayList<>();
    private OrderListAdapter mAdapter;
    private PageWrapper mPageWrapper;
    private boolean isVipEnter = false;
    private String mParam2;
    private boolean mParam1;

    private String useid;
    private String searchValue; /*搜索的值*/
    private boolean  isSearchAll;
    private boolean  isloadFinish;
    public static PickClothingUserInfoRightFragment newInstance(boolean isVipEnter, String searchValue, String id) {
        PickClothingUserInfoRightFragment fragment = new PickClothingUserInfoRightFragment();
        Bundle args = new Bundle();
        args.putBoolean(ARG_PARAM1, isVipEnter);
        args.putString(ARG_PARAM2, searchValue);
        args.putString(ARG_PARAM3, id);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            isVipEnter = getArguments().getBoolean(ARG_PARAM1, false);
            searchValue = getArguments().getString(ARG_PARAM2);
            useid = getArguments().getString(ARG_PARAM3);
        }
    }


    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        refreshLayout.startRefresh();

    }

    @Override
    protected OrderListPesenter createPresenter() {
        return new OrderListPesenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pick_clothing_user_info_right, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initListener();
        return view;
    }

    @Override
    protected void initView() {
        if (isVipEnter) {
            titleLayout.setVisibility(View.GONE);
        }
        mAdapter = new OrderListAdapter(activity, items);
        mRecycler.setLayoutManager(new LinearLayoutManager(activity));
        mRecycler.setAdapter(mAdapter);
        IPage page = new IPage() {
            @Override
            public void load(int pageIndex, int pageSize) {

                if (CheckUtils.isNull(searchValue)) return;
                 isloadFinish=false;
                if (isSearchAll){
                    mvpPresenter.takeList(_mActivity, searchValue, useid, pageIndex, pageSize);

                }else{
                    mvpPresenter.takeList(_mActivity, searchValue, "", pageIndex, pageSize);
                }


            }
        };
        mPageWrapper = new PageWrapper<>(mIAdapter, page);

    }

    IAdapter mIAdapter = new IAdapter() {
        @Override
        public void setDataSource(List data, boolean isNextLoad) {

            mAdapter.setDatas(data);
            refreshLayout.finishRefreshing();
            refreshLayout.setEnableLoadmore(isNextLoad);

        }

        @Override
        public void loadMoreFinish(List data, boolean isNextLoad) {
            mAdapter.addDatas(data);
            refreshLayout.finishLoadmore();
            refreshLayout.setEnableLoadmore(isNextLoad);

        }
    };

    @Override
    protected void initListener() {
        super.initListener();
        mAdapter.setOnItemClickListener(mOnItemClickListener);
        refreshLayout.setOnRefreshListener(mRefreshListenerAdapter);
    }

    private RefreshListenerAdapter mRefreshListenerAdapter = new RefreshListenerAdapter() {
        @Override
        public void onRefresh(TwinklingRefreshLayout refreshLayout) {
            super.onRefresh(refreshLayout);

            mPageWrapper.loadPage(true);
        }

        @Override
        public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
            super.onLoadMore(refreshLayout);

            mPageWrapper.loadPage(false);

        }
    };


    private OnItemClickListener mOnItemClickListener = new MyOnItemClickListener() {

        @Override
        public void onItemClick(ViewGroup viewGroup, View view, Object o, int i) {
            super.onItemClick(viewGroup, view, o, i);
            OrdersList.PageInfoBean.ListBean mItem = (OrdersList.PageInfoBean.ListBean) o;
            OrdersDetailFragment addAddress = OrdersDetailFragment.newInstance(mItem.getId()+"", "");
            StackManager.getInstance().popFragmant(_mActivity, PickClothingUserInfoRightFragment.this, addAddress, R.id.user_info_right_container);


        }
    };


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void loadSuccess(OrdersList mData) {
        isloadFinish=true;
        //mPageWrapper.addDataSource(mData.PageInfoBeangetList(), mData.getPages());
        mPageWrapper.addDataSource(mData.getPageInfo().getList(), mData.getPageInfo().getPages());
        showAllbtn.setVisibility(mData.isShowAllBtn()?View.VISIBLE:View.GONE);



    }

    @Override
    public void loadFail(String message) {
        refreshLayout.finishRefreshing();
        refreshLayout.finishLoadmore();
        T.show(message);
    }

    @OnClick({R.id.showAllbtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.showAllbtn:
                if (isloadFinish){
                    isSearchAll=true;
                    refreshLayout.startRefresh();

                }

                break;

        }
    }
}
